package com.gitee.yusugar.pay.alipay.core;

import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayObject;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.gitee.yusugar.common.pojo.DataRes;
import lombok.extern.slf4j.Slf4j;

/**
 * 手机网站支付
 */
@Slf4j
public class AlipayWapService extends AlipayService {

    @Override
    public DataRes<String> tradePay( AlipayObject model, String notifyUrl, String returnUrl ) {
        AlipayClient alipayClient = super.alipayConfig.buildAlipayClient();

        AlipayTradeWapPayRequest wapPayRequest = new AlipayTradeWapPayRequest();
        wapPayRequest.setNotifyUrl( StrUtil.isBlank( notifyUrl ) ? super.alipayConfig.getNotifyUrl() : notifyUrl );
        wapPayRequest.setReturnUrl( StrUtil.isBlank( returnUrl ) ? super.alipayConfig.getReturnUrl() : returnUrl );

        wapPayRequest.setBizModel( model );

        try {
            AlipayTradeWapPayResponse response = alipayClient.pageExecute( wapPayRequest );

            if ( response.isSuccess() )
                return DataRes.data( response.getBody() );

            return errorOrderNoRecord( response, response.getOutTradeNo(), "订单支付异常" );
        } catch ( AlipayApiException e ) {
            return errorRecord( e, "订单支付异常" );
        }
    }
}

